package com.dalin.platform.autocode.service.imple;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.dalin.common.PropertyConst;
import com.dalin.common.clazz.ArgList;
import com.dalin.common.clazz.Clazz;
import com.dalin.common.clazz.ImportList;
import com.dalin.common.clazz.Interfazz;
import com.dalin.common.clazz.Method;
import com.dalin.common.clazz.Note;
import com.dalin.common.clazz.Property;
import com.dalin.common.clazz.PropertyList;
import com.dalin.common.pager.PageInfo;
import com.dalin.common.utils.NumberUtils;
import com.dalin.common.utils.StringUtils;
import com.dalin.framework.dao.ConnectFactory;
import com.dalin.framework.dao.DbConn;
import com.dalin.framework.service.imple.FrameServiceImple;
import com.dalin.framework.vos.JdbcVo;
import com.dalin.platform.autocode.AutocodeConst;
import com.dalin.platform.autocode.condition.GlbDicItemRepositoryCondition;
import com.dalin.platform.autocode.condition.GlbDicTypeRepositoryCondition;
import com.dalin.platform.autocode.condition.GlbGeneratorKeyCondition;
import com.dalin.platform.autocode.condition.GlbGridHeaderCondition;
import com.dalin.platform.autocode.service.AutocodeService;
import com.dalin.platform.autocode.vo.EditMenuTreeVo;
import com.dalin.platform.autocode.vo.GlbDicItemRepositoryTreeVo;
import com.dalin.platform.autocode.vo.GlbDicTypeRepositoryVo;
import com.dalin.platform.autocode.vo.GlbGeneratorKeyVo;
import com.dalin.platform.autocode.vo.GlbGridHeaderVo;
import com.dalin.platform.etl.model.EtlColumnInfoEntity;
import com.dalin.platform.etl.model.EtlColumnViewEntity;
import com.dalin.platform.etl.model.EtlDatabaseInfoEntity;
import com.dalin.platform.etl.model.EtlTableInfoEntity;
import com.dalin.platform.glb.model.GlbDicItemRepositoryEntity;
import com.dalin.platform.glb.model.GlbDicTypeRepositoryEntity;
import com.dalin.platform.glb.model.GlbGeneratorKeyEntity;
import com.dalin.platform.loa.LoaConst;
import com.dalin.platform.loa.model.LoaMenuEntity;
import com.dalin.platform.loa.model.LoaPublicHeaderEntity;

/*****************************************************************************
 * @content: 系统设置等通用接口具体实现                                                                                                *
 * @author : 那建林                                                                                                                                 *
 * @crtDate: 2010-05-12                                                     *
 * @version: 1.0                                                            *
 ****************************************************************************/
public class AutocodeServiceImple 
		extends FrameServiceImple 
			implements AutocodeService {
	public void addUpDelMenus(List<EditMenuTreeVo> treeList, Long[] delIdArray)
			throws Exception {
		Date currentDate = new Date();
		LoaMenuEntity enty = null;
		String projectId = "";
		String parentId = "";
		int sequenceValue = 1;
		
		if (delIdArray != null && delIdArray.length > 0) {
			for (Long id : delIdArray) {
				if (id == null) continue;
				enty = (LoaMenuEntity) getDao()
						.findObjById(LoaMenuEntity.class, id);
				enty.setValid(0);
				getDao().updateObj(enty);
			}
		}
		
		for (EditMenuTreeVo vo : treeList) {
			if (StringUtils.isBlank(vo.getParentId())) continue;
			if (vo.getParentId().indexOf("~") > 0) {
				projectId = vo.getParentId().split("~")[0];
				parentId = vo.getParentId().split("~")[1];
			}
			if (vo.getId().startsWith("newId-")) {
				enty = new LoaMenuEntity();
				enty.setProjectId(Long.parseLong(projectId));
				enty.setMenuValue(vo.getMenuValue());
				enty.setMenuName(vo.getText());
				enty.setLinkPath(vo.getHref());
				enty.setParentId(Long.parseLong(parentId));
				enty.setSequence(sequenceValue++);
				enty.setValid(1);
				enty.setCreateDate(currentDate);
				getDao().createObj(enty);
				parentId = enty.getMenuId()+"";
			} else {
				String menuId = vo.getId().split("~")[1];
				enty = (LoaMenuEntity) getDao().findObjById(
						LoaMenuEntity.class, Long.parseLong(menuId));
				enty.setParentId(Long.parseLong(parentId));
				enty.setMenuValue(vo.getMenuValue());
				enty.setMenuName(vo.getText());
				enty.setLinkPath(vo.getHref());
				enty.setRemark(vo.getRemark());
				enty.setSequence(sequenceValue++);
				enty.setModifyDate(currentDate);
				getDao().updateObj(enty);
			}
		}
	}
	
	public PageInfo queryGlbGeneratorKeys(GlbGeneratorKeyCondition cond)throws Exception {
        PageInfo pageInfo = queryPageInfoByCond(cond.prepareParams());
        List<GlbGeneratorKeyEntity> entyList = pageInfo.getDataList();
        List<GlbGeneratorKeyVo> voList = new ArrayList<GlbGeneratorKeyVo>();
        GlbGeneratorKeyVo vo = null;
        for (GlbGeneratorKeyEntity enty : entyList) {
            vo = new GlbGeneratorKeyVo();
	        vo.setGenId(enty.getGenId()); // 
	        vo.setGenKey(enty.getGenKey()); // 
	        vo.setGenValue(enty.getGenValue()); // 
	        vo.setGenRemark(enty.getGenRemark()); // 
	        voList.add(vo);
        }
        pageInfo.setDataList(voList);
        return pageInfo;
    }

    
    public GlbGeneratorKeyVo findGlbGeneratorKeyById(Long id)throws Exception {
        GlbGeneratorKeyEntity enty = (GlbGeneratorKeyEntity) getDao().findObjById(GlbGeneratorKeyEntity.class, id);
        GlbGeneratorKeyVo vo = new GlbGeneratorKeyVo();
        vo.setGenId(enty.getGenId()); // 
        vo.setGenKey(enty.getGenKey()); // 
        vo.setGenValue(enty.getGenValue()); // 
        vo.setGenRemark(enty.getGenRemark()); // 
        return vo;
    }

   
    public void addGlbGeneratorKey(GlbGeneratorKeyVo vo) throws Exception {
        GlbGeneratorKeyEntity enty = new GlbGeneratorKeyEntity();
        enty = getGlbGeneratorKeyEntityValue(enty, vo);
        int size = getDao()
        		.queryAllObjs(GlbGeneratorKeyEntity.class).size() + 1;
        enty.setGenId(Long.parseLong(size+""));
        getDao().createObj(enty);
    }

    
    public void modifyGlbGeneratorKey(GlbGeneratorKeyVo vo)throws Exception {
        Long id = vo.getGenId();
        GlbGeneratorKeyEntity enty = (GlbGeneratorKeyEntity) getDao()
        		.findObjById(GlbGeneratorKeyEntity.class, id);
        enty = getGlbGeneratorKeyEntityValue(enty, vo);
        getDao().updateObj(enty);
    }

    
    public void delGlbGeneratorKeysByIds(Long[] idArray)throws Exception {
        getDao().deleteObjs(GlbGeneratorKeyEntity.class, idArray);
    }

    private GlbGeneratorKeyEntity getGlbGeneratorKeyEntityValue(GlbGeneratorKeyEntity enty, GlbGeneratorKeyVo vo) {
        enty.setGenKey(vo.getGenKey()); // 
        enty.setGenValue(vo.getGenValue()); // 
        enty.setGenRemark(vo.getGenRemark()); // 
        return enty;
    }


    public PageInfo queryGlbDicTypeRepositorys(GlbDicTypeRepositoryCondition cond)throws Exception {
        PageInfo pageInfo = queryPageInfoByCond(cond.prepareParams());
        List<GlbDicTypeRepositoryEntity> entyList = pageInfo.getDataList();
        List<GlbDicTypeRepositoryVo> voList = new ArrayList<GlbDicTypeRepositoryVo>();
        GlbDicTypeRepositoryVo vo = null;
        for (GlbDicTypeRepositoryEntity enty : entyList) {
            vo = new GlbDicTypeRepositoryVo();
	        vo.setTypeId(enty.getTypeId()); // 
	        vo.setTypeCode(enty.getTypeCode()); // 
	        vo.setTypeDescription(enty.getTypeDescription()); // 
	        voList.add(vo);
	    }
        pageInfo.setDataList(voList);
        return pageInfo;
    }

  
    public GlbDicTypeRepositoryVo findGlbDicTypeRepositoryById(Long id)throws Exception {
        GlbDicTypeRepositoryEntity enty = (GlbDicTypeRepositoryEntity) getDao().findObjById(GlbDicTypeRepositoryEntity.class, id);
        GlbDicTypeRepositoryVo vo = new GlbDicTypeRepositoryVo();
        vo.setTypeId(enty.getTypeId()); // 
        vo.setTypeCode(enty.getTypeCode()); // 
        vo.setTypeDescription(enty.getTypeDescription()); // 
        return vo;
    }
   
    public void addGlbDicTypeRepository(GlbDicTypeRepositoryVo vo) throws Exception {
        GlbDicTypeRepositoryEntity enty = new GlbDicTypeRepositoryEntity();
        enty = getGlbDicTypeRepositoryEntityValue(enty, vo);
        getDao().createObj(enty);
    }

    public void modifyGlbDicTypeRepository(GlbDicTypeRepositoryVo vo)throws Exception {
        Long id = vo.getTypeId();
        GlbDicTypeRepositoryEntity enty = (GlbDicTypeRepositoryEntity) getDao().findObjById(GlbDicTypeRepositoryEntity.class, id);
        enty = getGlbDicTypeRepositoryEntityValue(enty, vo);
        getDao().updateObj(enty);
    }
   
    public void delGlbDicTypeRepositorysByIds(Long[] idArray)throws Exception {
        getDao().deleteObjs(GlbDicTypeRepositoryEntity.class, idArray);
    }

    private GlbDicTypeRepositoryEntity getGlbDicTypeRepositoryEntityValue(GlbDicTypeRepositoryEntity enty, GlbDicTypeRepositoryVo vo) {
        enty.setTypeCode(vo.getTypeCode()); // 
        enty.setTypeDescription(vo.getTypeDescription()); // 
        return enty;
    }
    
    public void addUpDelDicItemRepositorys(Long typeId, List<GlbDicItemRepositoryTreeVo> treeList, Long[] delIdArray) throws Exception {
    	Date currentDate = new Date();
		GlbDicItemRepositoryEntity enty = null;
		String parentId = "";
		Integer maxItemCode = 1;
		int sequenceValue = 1;
		
		GlbDicItemRepositoryCondition cond = new GlbDicItemRepositoryCondition();
		cond.setTypeId(typeId);
		List<GlbDicItemRepositoryEntity> dataList = getDao().queryObjsToPages(
				cond.prepareMaxItemCode().getPreparedPageQuery());
		if (!StringUtils.isBlank(dataList)) {
			maxItemCode = dataList.get(0).getItemCode() + 1;
		}
		
		if (delIdArray != null && delIdArray.length > 0) {
			for (Long id : delIdArray) {
				if (id == null) continue;
				enty = (GlbDicItemRepositoryEntity) getDao()
						.findObjById(GlbDicItemRepositoryEntity.class, id);
				getDao().updateObj(enty);
			}
		}
		for (GlbDicItemRepositoryTreeVo vo : treeList) {
			if (NumberUtils.isIntegerType(vo.getParentId())) {
				parentId = vo.getParentId();
			}
			if (vo.getId().startsWith("newId-")) {
				enty = new GlbDicItemRepositoryEntity();
				enty.setTypeId(typeId);
				enty.setParentItemCode(Integer.parseInt(parentId));
				if (StringUtils.isBlank(vo.getHref())) {
					enty.setItemCode(maxItemCode++);
				} else {
					enty.setItemCode(Integer.parseInt(vo.getHref()));
				}
				enty.setItemName(vo.getText());
				enty.setRemark(vo.getRemark());
				enty.setOrderNumber(sequenceValue++);
				getDao().createObj(enty);
				parentId = enty.getParentItemCode() + "";
			} else {
				enty = (GlbDicItemRepositoryEntity) getDao().findObjById(
						GlbDicItemRepositoryEntity.class, Long.parseLong(vo.getId()));
				enty.setParentItemCode(Integer.parseInt(parentId));
				if (StringUtils.isBlank(vo.getHref())) {
					enty.setItemCode(maxItemCode++);
				} else {
					enty.setItemCode(Integer.parseInt(vo.getHref()));
				}
				enty.setItemName(vo.getText());
				enty.setRemark(vo.getRemark());
				enty.setOrderNumber(sequenceValue++);
				getDao().updateObj(enty);
			}
		}
    }
    
    
    
    public PageInfo queryGlbGridHeaders(GlbGridHeaderCondition cond)throws Exception {
        PageInfo pageInfo = queryPageInfoByCond(cond.prepareParams());
        List<LoaPublicHeaderEntity> entyList = pageInfo.getDataList();
        List<GlbGridHeaderVo> voList = new ArrayList<GlbGridHeaderVo>();
        GlbGridHeaderVo vo = null;
        for (LoaPublicHeaderEntity enty : entyList) {
            vo = new GlbGridHeaderVo();
            vo.setHeaderId(enty.getHeaderId()); // 
	        vo.setMenuName(enty.getLoaMenuEntity().getMenuName()); // 
	        vo.setLabelName(enty.getLabelName()); // 
	        vo.setPropertyName(enty.getPropertyName()); // 
	        voList.add(vo);
        }
        pageInfo.setDataList(voList);
        return pageInfo;
    }

    public GlbGridHeaderVo findGlbGridHeaderById(Long id)throws Exception {
        LoaPublicHeaderEntity enty = getDao().findObjById(LoaPublicHeaderEntity.class, id);
        GlbGridHeaderVo vo = new GlbGridHeaderVo();
        vo.setMenuId(enty.getMenuId()); // 
        vo.setHeaderId(enty.getHeaderId()); // 
        vo.setLabelName(enty.getLabelName()); // 
        vo.setPropertyName(enty.getPropertyName()); // 
        return vo;
    }

    public void addGlbGridHeader(GlbGridHeaderVo vo)throws Exception {
        LoaPublicHeaderEntity enty = new LoaPublicHeaderEntity();
        enty = getLoaPublicHeaderEntityValue(enty, vo);
        getDao().createObj(enty);
    }

    public void modifyGlbGridHeader(GlbGridHeaderVo vo)throws Exception {
        Long id = vo.getHeaderId();
        LoaPublicHeaderEntity enty = (LoaPublicHeaderEntity) getDao().findObjById(LoaPublicHeaderEntity.class, id);
        enty = getLoaPublicHeaderEntityValue(enty, vo);
        getDao().updateObj(enty);
    }

    public void delGlbGridHeadersByIds(Long[] idArray)throws Exception {
        getDao().deleteObjs(LoaPublicHeaderEntity.class, idArray);
    }

    private LoaPublicHeaderEntity getLoaPublicHeaderEntityValue(LoaPublicHeaderEntity enty, GlbGridHeaderVo vo) {
        enty.setMenuId(vo.getMenuId()); // 
        enty.setLabelName(vo.getLabelName()); // 
        enty.setPropertyName(vo.getPropertyName()); // 
        return enty;
    }

    
    
    
    
    public List<Method> getGetSetMethodList(PropertyList propList) {
		List<Method> methodList = new ArrayList<Method>();
		Method method = null;
		StringBuffer buf = null;
		for (Property p : propList.getAll()) {
			String pType = p.getType();
			String pName = p.getName();
			String upperPName = pName.substring(0, 1).toUpperCase()
					+ pName.substring(1);
			// getMethod
			buf = new StringBuffer();
			buf.append(PropertyConst.TAB).append(PropertyConst.TAB)
				.append(PropertyConst.RETURN_STR)
				.append(PropertyConst.SPACE).append(pName)
				.append(PropertyConst.SEMICOLON);
			method = new Method(PropertyConst.GET_STR + upperPName);
			method.setBody(buf.toString());
			method.setRtnType(pType);
			methodList.add(method);
			
			// setMethod
			buf = new StringBuffer();
			buf.append(PropertyConst.TAB).append(PropertyConst.TAB)
				.append(PropertyConst.THIS_STR).append(PropertyConst.DOT)
				.append(pName).append(PropertyConst.SPACE)
				.append(PropertyConst.EQUAL).append(PropertyConst.SPACE)
				.append(pName).append(PropertyConst.SEMICOLON);
			ArgList argList = new ArgList();
			argList.add(pType, pName);
			method = new Method(PropertyConst.SET_STR + upperPName);
			method.setArgList(argList);
			method.setBody(buf.toString());
			methodList.add(method);
		}
		return methodList;
	}

    public Method computeEqualsMethod(Class c) {
    	StringBuffer body = new StringBuffer();
		body.append(PropertyConst.TWO_TAB).append("if (this == o) return true;")
			.append(PropertyConst.LINE_SEPARATOR)
			.append(PropertyConst.TWO_TAB).append("if (!(o instanceof ")
			.append(c.getSimpleName()).append(")) return false;")
			.append(PropertyConst.LINE_SEPARATOR)
			.append(PropertyConst.TWO_TAB).append("final ")
			.append(c.getSimpleName()).append(" other = (")
			.append(c.getSimpleName()).append(") o;")
			.append(PropertyConst.LINE_SEPARATOR);
		for (Field d : c.getDeclaredFields()) {
			body.append(PropertyConst.TWO_TAB).append("if (")
				.append(d.getName()).append(" == null) {")
				.append(PropertyConst.LINE_SEPARATOR)
				.append(PropertyConst.THREE_TAB).append("if (other.")
				.append(d.getName()).append(" != null) return false;")
				.append(PropertyConst.LINE_SEPARATOR)
			
				.append(PropertyConst.TWO_TAB).append("} else if (!")
				.append(d.getName()).append(".equals(other.").append(d.getName())
				.append(")) {").append(PropertyConst.LINE_SEPARATOR)
				.append(PropertyConst.THREE_TAB).append("return false;")
				.append(PropertyConst.LINE_SEPARATOR)
				.append(PropertyConst.TWO_TAB).append("}")
				.append(PropertyConst.LINE_SEPARATOR);
		}
		body.append(PropertyConst.TWO_TAB).append("return true;");
		Method rtnMethod = new Method("equals");
		rtnMethod.setMethodNote("@Override");
		rtnMethod.setArgList(new ArgList().add("Object", "o"));
		rtnMethod.setBody(body.toString());
		rtnMethod.setRtnType("boolean");
		return rtnMethod;
	}

    public Method computeEqualsMethod(Clazz c) {
		List<Property> allProp = c.getPropertyList().getAll();
		StringBuffer body = new StringBuffer();
		body.append(PropertyConst.TWO_TAB).append("if (this == o) return true;")
			.append(PropertyConst.LINE_SEPARATOR)
			.append(PropertyConst.TWO_TAB).append("if (!(o instanceof ")
			.append(c.getClassName()).append(")) return false;")
			.append(PropertyConst.LINE_SEPARATOR)
			.append(PropertyConst.TWO_TAB).append("final ")
			.append(c.getClassName()).append(" other = (")
			.append(c.getClassName()).append(") o;")
			.append(PropertyConst.LINE_SEPARATOR);
		for (Property p : allProp) {
			body.append(PropertyConst.TWO_TAB).append("if (")
				.append(p.getName()).append(" == null) {")
				.append(PropertyConst.LINE_SEPARATOR)
				.append(PropertyConst.THREE_TAB).append("if (other.")
				.append(p.getName()).append(" != null) return false;")
				.append(PropertyConst.LINE_SEPARATOR)
			
				.append(PropertyConst.TWO_TAB).append("} else if (!")
				.append(p.getName()).append(".equals(other.").append(p.getName())
				.append(")) {").append(PropertyConst.LINE_SEPARATOR)
				.append(PropertyConst.THREE_TAB).append("return false;")
				.append(PropertyConst.LINE_SEPARATOR)
				.append(PropertyConst.TWO_TAB).append("}")
				.append(PropertyConst.LINE_SEPARATOR);
		}
		body.append(PropertyConst.TWO_TAB).append("return true;");
		Method rtnMethod = new Method("equals");
		rtnMethod.setMethodNote("@Override");
		rtnMethod.setArgList(new ArgList().add("Object", "o"));
		rtnMethod.setBody(body.toString());
		rtnMethod.setRtnType("boolean");
		return rtnMethod;
	}

	public Method computeHashCodeMethod(Class c) {
		StringBuffer body = new StringBuffer();
		body.append(PropertyConst.TWO_TAB).append("int njlrst = 17;")
			.append(PropertyConst.LINE_SEPARATOR);
		for (Field d : c.getDeclaredFields()) {
			body.append(PropertyConst.TWO_TAB).append("njlrst = 31 * njlrst + (")
				.append(d.getName()).append(" == null ? 0 : ")
				.append(d.getName()).append(".hashCode());")
			.append(PropertyConst.LINE_SEPARATOR);
		}
		body.append(PropertyConst.TWO_TAB).append("return njlrst;");
		Method rtnMethod = new Method("hashCode");
		rtnMethod.setMethodNote("@Override");
		rtnMethod.setBody(body.toString());
		rtnMethod.setRtnType("int");
		return rtnMethod;
	}

	public Method computeHashCodeMethod(Clazz c) {
		List<Property> allProp = c.getPropertyList().getAll();
		StringBuffer body = new StringBuffer();
		body.append(PropertyConst.TWO_TAB).append("int njlrst = 17;")
			.append(PropertyConst.LINE_SEPARATOR);
		for (Property p : allProp) {
			body.append(PropertyConst.TWO_TAB).append("njlrst = 31 * njlrst + (")
				.append(p.getName()).append(" == null ? 0 : ")
				.append(p.getName()).append(".hashCode());")
			.append(PropertyConst.LINE_SEPARATOR);
		}
		body.append(PropertyConst.TWO_TAB).append("return njlrst;");
		Method rtnMethod = new Method("hashCode");
		rtnMethod.setMethodNote("@Override");
		rtnMethod.setBody(body.toString());
		rtnMethod.setRtnType("int");
		return rtnMethod;
	}
    
	public Clazz getJavaClazz(Long tableId, String packageName) throws Exception {
		EtlTableInfoEntity tableEnty = (EtlTableInfoEntity) getDao()
				.findObjById(EtlTableInfoEntity.class, tableId);
		List<EtlColumnInfoEntity> columnEntyList = null;
		columnEntyList = tableEnty.getEtlColumnInfoEntitys();
		PropertyList propList = new PropertyList();
		JdbcVo jdbc = this.getJdbcVoFromDatabaseInfoEnty(
				tableEnty.getDatabaseInfoEntity());
		DbConn conn = ConnectFactory.getConnectionInstance(jdbc);
		
		for (EtlColumnInfoEntity enty : columnEntyList) {
			String lowerName = StringUtils
					.underlineToLowerCamelCase(enty.getColumnName());
			String javaType = conn.getJavaTypeFromColumnType(
					LoaConst.findItemName(LoaConst
			        		.DIC_TYPE_CODE_ARRAY[1], enty.getColumnType()+""),
					enty.getColumnLength(), 
					enty.getDecimalDigits());
			StringBuffer noteBuf = new StringBuffer();
			noteBuf.append(enty.getColumnName()).append(PropertyConst.COMMA)
					.append(enty.getColumnLength()).append(PropertyConst.COMMA)
					.append(enty.getDecimalDigits()).append(PropertyConst.COMMA)
					.append(enty.getColumnComment());
			propList.add(javaType, lowerName, noteBuf.toString());
		}

		Clazz myClass = new Clazz();
		myClass.setPackageName(packageName);
		myClass.setClassName(
				StringUtils.underlineToUpperCamelCase(tableEnty.getTableName()) + "Vo");
		myClass.setPropertyList(propList);
		myClass.setMethodList(getGetSetMethodList(propList));
		return myClass;
	}
	
	private JdbcVo getJdbcVoFromDatabaseInfoEnty(EtlDatabaseInfoEntity enty) {
		JdbcVo jdbc = new JdbcVo();
		jdbc.setHostIp(enty.getHostIp());          // 主机IP
		jdbc.setPort(enty.getPort() + "");            // 端口号
		jdbc.setDbName(enty.getDatabaseName());      // 数据库名
		jdbc.setDriverClassName(LoaConst.findItemName(
				LoaConst.DIC_TYPE_CODE_ARRAY[0], enty.getDatabaseDriver()+""));
		jdbc.setUserName(enty.getUserName());        // 用户名
		jdbc.setUserPswd(enty.getUserPswd());        // 密码
		return jdbc;
	}

	public Clazz getAnnotationClazz(Long tableId, String packageName)
			throws Exception {
		String primaryKeyProName = null;
		EtlTableInfoEntity tableEnty = (EtlTableInfoEntity) getDao()
				.findObjById(EtlTableInfoEntity.class, tableId);
		List<EtlColumnInfoEntity> columnList = tableEnty.getEtlColumnInfoEntitys();
		for (EtlColumnInfoEntity ce : columnList) {
			if (ce.getIsPrimaryKey()) {
				primaryKeyProName = StringUtils.underlineToLowerCamelCase(ce.getColumnName());
			}
		}
		String tableName = tableEnty.getTableName();
		Clazz myClass = getJavaClazz(tableId, packageName);
		ImportList importList = new ImportList();
		importList.add("java.io.Serializable")
				.add("javax.persistence.Column")
				.add("javax.persistence.Entity")
				.add("javax.persistence.GeneratedValue")
				.add("javax.persistence.GenerationType")
				.add("javax.persistence.Id")
				.add("javax.persistence.Table")
				.add("javax.persistence.TableGenerator");
		myClass.setImportList(importList);
		myClass.setNote("@Entity" + PropertyConst.LINE_SEPARATOR
				+ "@Table(name=\"" + tableName + "\")"
				+ PropertyConst.LINE_SEPARATOR);
		myClass.setExtendsClassName("com.dalin.common.base.BaseObject");
		
		PropertyList propList = myClass.getPropertyList();
		List<Method> methodList = myClass.getMethodList();
		for (Property p : propList.getAll()) {
			String[] noteArray = StringUtils
				.getStrArrayFromComma(p.getNote());
			String methodNote = Note.getHibernateAnnotation(
					noteArray[0], p.getType(), Integer.parseInt(noteArray[1]));
			if (p.getName().equals(primaryKeyProName)) {
				StringBuffer buf = new StringBuffer();
				buf.append("@Id").append(PropertyConst.LINE_SEPARATOR)
					.append(PropertyConst.TAB).append(methodNote).append(PropertyConst.LINE_SEPARATOR)
					.append(PropertyConst.TAB).append("@GeneratedValue(strategy = GenerationType.TABLE,generator=\"generator_normal\")").append(PropertyConst.LINE_SEPARATOR)
					.append(PropertyConst.TAB).append("@TableGenerator(").append(PropertyConst.LINE_SEPARATOR)
					.append(PropertyConst.TWO_TAB).append("name = \"generator_normal\",").append(PropertyConst.LINE_SEPARATOR)
					.append(PropertyConst.TWO_TAB).append("table = \"GLB_GENERATOR_KEY\",").append(PropertyConst.LINE_SEPARATOR)
					.append(PropertyConst.TWO_TAB).append("pkColumnName = \"GEN_KEY\",").append(PropertyConst.LINE_SEPARATOR)
					.append(PropertyConst.TWO_TAB).append("valueColumnName = \"GEN_VALUE\",").append(PropertyConst.LINE_SEPARATOR)
					.append(PropertyConst.TWO_TAB).append("pkColumnValue = \"" + tableName + "\",").append(PropertyConst.LINE_SEPARATOR)
					.append(PropertyConst.TWO_TAB).append("allocationSize=1").append(PropertyConst.LINE_SEPARATOR)
					.append(PropertyConst.TAB).append(")");
				for (Method m : methodList) {
					if (m.getMethodName().equalsIgnoreCase(
							PropertyConst.GET_STR + p.getName())) {
						m.setMethodNote(buf.toString());
						break;
					}
				}
				continue;
			}
			for (Method m : methodList) {
				if (m.getMethodName().equalsIgnoreCase(
						PropertyConst.GET_STR + p.getName())) {
					m.setMethodNote(methodNote);
					break;
				}
			}
		}
		myClass.setClassName(
				StringUtils.underlineToUpperCamelCase(tableName) + "Entity");
		methodList.add(computeEqualsMethod(myClass));
		methodList.add(computeHashCodeMethod(myClass));
		return myClass;
	}

	public Clazz getConditionClazz(Long tableId, String packageName)
			throws Exception {
		EtlTableInfoEntity tableEnty = (EtlTableInfoEntity) getDao()
				.findObjById(EtlTableInfoEntity.class, tableId);
		String tableName = tableEnty.getTableName();
		Clazz cls = getJavaClazz(tableId, packageName);
		// 引用哪些包
		ImportList importList = new ImportList();
			importList.add("java.io.Serializable")
			.add("com.dalin.framework.pager.Condition")
			.add("com.dalin.common.utils.StringUtils")
			.add("com.dalin.common.utils.DateUtils");
		cls.setImportList(importList);
		
		//类名
		cls.setClassName(StringUtils.underlineToUpperCamelCase(tableName)
				+ "Condition");
		cls.setExtendsClassName("Condition");
		
		List<Method> methodList = cls.getMethodList();
		methodList.clear();
		/**
		 * 增加 protected void prepareParams()方法
		 */ 
		Method preMethod = new Method("prepareParams");
		preMethod.setRtnType(cls.getClassName());
		StringBuffer bodyBuf = new StringBuffer(); // 方法体
		bodyBuf.append(PropertyConst.TWO_TAB)
			.append("setBaseQueryStr(\"from ")
			.append(StringUtils.underlineToUpperCamelCase(tableName)+"Entity")
			.append(" h\");").append(PropertyConst.LINE_SEPARATOR);
		PropertyList proList = cls.getPropertyList();
		PropertyList newPropList = new PropertyList();
		//先组织查询语句，再把类属性全变为String类型
		for (Property p : proList.getAll()) {
			String propType = p.getType();
			String propName = p.getName();
			if (PropertyConst.STRING_STR.equals(propType)) {
				bodyBuf.append(PropertyConst.TWO_TAB).append("setParam(\"h.")
					.append(propName).append("\", \"like\", \"%\" + ")
					.append(propName).append(" + \"%\");")
					.append(PropertyConst.LINE_SEPARATOR);
				newPropList.add(p);
			} else {
				bodyBuf.append(PropertyConst.TWO_TAB).append("setParam(\"h.")
					.append(propName).append("\", \">=\", ");
				bodyBuf.append(propName + 1)
					.append(");").append(PropertyConst.LINE_SEPARATOR);
				p.setName(propName + 1);
				newPropList.add(new Property(p.getType(), p.getName(), p.getNote()));
				bodyBuf.append(PropertyConst.TWO_TAB).append("setParam(\"h.")
					.append(propName).append("\", \"<\", ");
				bodyBuf.append(propName + 2)
					.append(");").append(PropertyConst.LINE_SEPARATOR);
				p.setName(propName + 2);
				newPropList.add(p);
			}
		}
		cls.setPropertyList(newPropList);
		bodyBuf.append(PropertyConst.TWO_TAB).append("return this;");
		preMethod.setBody(bodyBuf.toString());
		methodList.add(preMethod);
		cls.setMethodList(methodList);
		return cls;
	}

	public Interfazz getServiceInterfaceClazz(Long tableId,	String packageName) {
		EtlTableInfoEntity tableEnty = (EtlTableInfoEntity) getDao()
				.findObjById(EtlTableInfoEntity.class, tableId);
		String tableName = tableEnty.getTableName();
		String javaTableName = StringUtils.underlineToUpperCamelCase(tableName);
		Interfazz inter = new Interfazz();
		inter.setPackageName(packageName);
		inter.setInterfaceName(javaTableName + "Service");
		List<Method> methodList = new ArrayList<Method>();
		Method method = null;
		ArgList argList = new ArgList();
		/**
		 * 查询方法
		 */
		method = new Method("query" + javaTableName + "s");
		method.setRtnType("PageInfo");
		argList.add(javaTableName + "Condition", "cond");
		method.setArgList(argList);
		method.setMethodNote(Note.getMethodNote(method));
		method.setException("Exception");
		methodList.add(method);
		/**
		 * 查找单条记录方法
		 */
		method = new Method("find" + javaTableName + "ById");
		method.setRtnType(javaTableName + "Vo");
		argList = new ArgList();
		argList.add("Long", "id");
		method.setArgList(argList);
		method.setException("Exception");
		method.setMethodNote(Note.getMethodNote(method));
		methodList.add(method);
		/**
		 * 增加记录方法
		 */
		method = new Method("add" + javaTableName);
		argList = new ArgList();
		argList.add(javaTableName + "Vo", "vo");
		method.setArgList(argList);
		method.setException("Exception");
		method.setMethodNote(Note.getMethodNote(method));
		methodList.add(method);
		/**
		 * 修改记录方法
		 */
		method = new Method("modify" + javaTableName);
		argList = new ArgList();
		argList.add(javaTableName + "Vo", "vo");
		method.setArgList(argList);
		method.setException("Exception");
		method.setMethodNote(Note.getMethodNote(method));
		methodList.add(method);
		/**
		 * 删除方法
		 */
		method = new Method("del" + javaTableName + "sByIds");
		argList = new ArgList();
		argList.add("Long[]", "idArray");
		method.setArgList(argList);
		method.setException("Exception");
		method.setMethodNote(Note.getMethodNote(method));
		methodList.add(method);
		
		inter.setMethodList(methodList);
		return inter;
	}

	public Clazz getServiceImpleClazz(Long tableId, String packageName) throws Exception {
		EtlTableInfoEntity tableEnty = (EtlTableInfoEntity) getDao()
				.findObjById(EtlTableInfoEntity.class, tableId);
		String tableName = tableEnty.getTableName();
		String javaTableName = StringUtils.underlineToUpperCamelCase(tableName);
		PropertyList propList = getJavaClazz(tableId, packageName)
				.getPropertyList();
		Interfazz inter = getServiceInterfaceClazz(tableId, packageName);
		Clazz cls = new Clazz();
		cls.setPackageName(packageName);
		cls.setClassName(inter.getInterfaceName() + "Imple");
		cls.setExtendsClassName("FrameServiceImple");
		List<String> implementsList = new ArrayList<String>();
		implementsList.add(inter.getInterfaceName());
		cls.setImplementsList(implementsList);
		cls.setMethodList(inter.getMethodList());
		
		Method method = null;
		StringBuffer buf = null;
		/**
		 * 查询方法
		 */
		method = cls.getMethodList().get(0);
		method.setException("Exception");
		buf = new StringBuffer();
		buf.append(PropertyConst.TWO_TAB)
			.append("PageInfo pageInfo = queryPageInfoByCond(cond.prepareParams());")
			.append(PropertyConst.LINE_SEPARATOR).append(PropertyConst.TWO_TAB)
			.append("List<").append(javaTableName).append("Entity")
			.append("> entyList = pageInfo.getDataList();")
			.append(PropertyConst.LINE_SEPARATOR).append(PropertyConst.TWO_TAB)
			.append("List<").append(javaTableName)
			.append("Vo> voList = new ArrayList<")
			.append(javaTableName).append("Vo>();")
			.append(PropertyConst.LINE_SEPARATOR).append(PropertyConst.TWO_TAB)
			.append(javaTableName).append("Vo vo = null;")
			.append(PropertyConst.LINE_SEPARATOR).append(PropertyConst.TWO_TAB);
		//for循环
		buf.append("for (").append(javaTableName)
			.append("Entity enty : entyList) {").append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.THREE_TAB).append("vo = new ")
			.append(javaTableName).append("Vo();")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(setPropertyValue(propList, "vo", "enty"));
		buf.append(PropertyConst.TWO_TAB).append("voList.add(vo);")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB)
			.append(PropertyConst.RIGHT_BRACE).append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB).append("pageInfo.setDataList(voList);")
			.append(PropertyConst.LINE_SEPARATOR).append(PropertyConst.TWO_TAB)
			.append("return pageInfo;");
		method.setBody(buf.toString());
		method.setMethodNote(null);
		cls.getMethodList().set(0, method);
		/**
		 * 查找单条记录方法
		 */
		method = cls.getMethodList().get(1);
		method.setException("Exception");
		buf = new StringBuffer();
		buf.append(PropertyConst.TWO_TAB).append(javaTableName + "Entity")
			.append(" enty = getDao().findObjById(")
			.append(javaTableName + "Entity.class, id);")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB).append(javaTableName + "Vo")
			.append(" vo = new ").append(javaTableName)
			.append("Vo();").append(PropertyConst.LINE_SEPARATOR);
		buf.append(setPropertyValue(propList, "vo", "enty"));
		buf.append(PropertyConst.TWO_TAB).append("return vo;");
		method.setBody(buf.toString());
		method.setMethodNote(null);
		cls.getMethodList().set(1, method);
		/**
		 * 增加记录方法
		 */
		method = cls.getMethodList().get(2);
		buf = new StringBuffer();
		buf.append(PropertyConst.TWO_TAB).append(javaTableName + "Entity")
			.append(" enty = new ").append(javaTableName + "Entity();")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB).append("enty = get")
			.append(javaTableName).append("EntityValue(enty, vo);")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB).append("getDao().createObj(enty);");
		method.setBody(buf.toString());
		method.setMethodNote(null);
		cls.getMethodList().set(2, method);
		/**
		 * 修改记录方法
		 */
		method = cls.getMethodList().get(3);
		method.setException("Exception");
		buf = new StringBuffer();
		buf.append(PropertyConst.TWO_TAB).append("Long id = vo.get")
			.append(javaTableName + "Id();").append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB).append(javaTableName + "Entity")
			.append(" enty = getDao().findObjById(")
			.append(javaTableName + "Entity.class, id);")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB).append("enty = get")
			.append(javaTableName).append("EntityValue(enty, vo);")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB).append("getDao().updateObj(enty);");
		method.setBody(buf.toString());
		method.setMethodNote(null);
		cls.getMethodList().set(3, method);
		/**
		 * 批量删除方法
		 */
		method = cls.getMethodList().get(4);
		method.setException("Exception");
		buf = new StringBuffer();
		buf.append(PropertyConst.TWO_TAB).append("getDao().deleteObjs(")
			.append(javaTableName).append("Entity.class, idArray);");
		method.setBody(buf.toString());
		method.setMethodNote(null);
		cls.getMethodList().set(4, method);
		/**
		 * 增加属性值copy方法
		 */
		method = new Method("get" + javaTableName + "EntityValue");
		method.setModifier(PropertyConst.PRIVATE_STR);
		method.setRtnType(javaTableName + "Entity");
		ArgList argList = new ArgList();
		argList.add(javaTableName + "Entity", "enty");
		argList.add(javaTableName + "Vo", "vo");
		method.setArgList(argList);
		method.setBody(setPropertyValue(propList, "enty", "vo"));
		cls.getMethodList().add(method);
		return cls;
	}
	private String setPropertyValue(PropertyList propList,
			String setRef, String getRef) {
		StringBuffer buf = new StringBuffer();
		for (Property p : propList.getAll()) {
			String pName = p.getName();
			pName = pName.substring(0, 1).toUpperCase() + pName.substring(1);
			buf.append(PropertyConst.TWO_TAB).append(setRef)
				.append(".set").append(pName).append("(").append(getRef)
				.append(".get").append(pName).append("()); // ")
				.append(PropertyConst.LINE_SEPARATOR);
		}
		return buf.toString();
	}

	public Clazz getAddModActionClazz(String serviceName, Long tableId,
			String packageName) throws Exception {
		EtlTableInfoEntity tableEnty = (EtlTableInfoEntity) getDao()
				.findObjById(EtlTableInfoEntity.class, tableId);
		String tableName = tableEnty.getTableName();
		String serviceRefName = serviceName.substring(0, 1).toLowerCase() 
				+ serviceName.substring(1);
		String upperJavaName = StringUtils.underlineToUpperCamelCase(tableName);
		PropertyList propertyList = new PropertyList();
		propertyList.add(new Property(serviceName, serviceRefName, null));
		propertyList.add(new Property(upperJavaName + "Vo", "vo", null));
		List<Method> methodList = new ArrayList<Method>();
		Method method = new Method("execute");
		method.setRtnType(PropertyConst.STRING_STR);
		method.setException("Exception");
		StringBuffer buf = new StringBuffer();
		buf.append(PropertyConst.TWO_TAB)
			.append("super.printInfo(\"\", \"===== this action begin\");")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append("        try {").append(PropertyConst.LINE_SEPARATOR);
		buf.append("        	 if (vo.get")
			.append(upperJavaName).append("Id() == null) {")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append("                 ").append(serviceRefName)
			.append(".add").append(upperJavaName)
			.append("(vo);").append(PropertyConst.LINE_SEPARATOR);
		buf.append("             } else {")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append("                 ").append(serviceRefName)
			.append(".modify").append(upperJavaName)
			.append("(vo);").append(PropertyConst.LINE_SEPARATOR);
		buf.append("             }")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append("setResContent(true, \"\");")
				.append(PropertyConst.LINE_SEPARATOR);
		buf.append("        } catch (Exception e) {")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append("        	e.printStackTrace();")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append("        	setResContent(e);")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append("        }")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB)
			.append("super.printInfo(\"\", \"===== this action end\");")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB)
		.append("return null;")
		.append(PropertyConst.LINE_SEPARATOR);
		method.setBody(buf.toString());
		methodList.add(method);
		List<Method> getSetMethodList = getGetSetMethodList(propertyList);
		for (Method m : getSetMethodList) {
			methodList.add(m);
		}
		
		Clazz cls = new Clazz();
		cls.setPackageName(packageName);
		ImportList importList = new ImportList();
		importList.add("com.dalin.framework.action.StrutsTwoAction");
		cls.setImportList(importList);
		cls.setClassName(upperJavaName + "AddModAction");
		cls.setExtendsClassName("StrutsTwoAction");
		cls.setPropertyList(propertyList);
		cls.setMethodList(methodList);
		return cls;
	}

	public Clazz getDelActionClazz(String serviceName, Long tableId,
			String packageName) throws Exception {
		EtlTableInfoEntity tableEnty = (EtlTableInfoEntity) getDao()
				.findObjById(EtlTableInfoEntity.class, tableId);
		String tableName = tableEnty.getTableName();
		String serviceRefName = serviceName.substring(0, 1).toLowerCase() 
				+ serviceName.substring(1);
		String upperJavaName = StringUtils.underlineToUpperCamelCase(tableName);
		PropertyList propertyList = new PropertyList();
		propertyList.add(new Property(serviceName, serviceRefName, null));
		propertyList.add(new Property("Long[]", "idArray", null));
		List<Method> methodList = new ArrayList<Method>();
		Method method = new Method("execute");
		method.setRtnType(PropertyConst.STRING_STR);
		method.setException("Exception");
		StringBuffer buf = new StringBuffer();
		buf.append(PropertyConst.TWO_TAB)
			.append("super.printInfo(\"\", \"===== this action begin\");")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB)
			.append("try {").append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.THREE_TAB).append(serviceRefName)
			.append(".del").append(upperJavaName)
			.append("sByIds(idArray);").append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.THREE_TAB)
			.append("setResContent(true, \"\");")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB)
			.append("} catch (Exception e) {")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.THREE_TAB)
			.append("e.printStackTrace();")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB)
			.append("}")
			.append(PropertyConst.LINE_SEPARATOR);	
		buf.append(PropertyConst.TWO_TAB)
			.append("super.printInfo(\"\", \"===== this action end\");")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB)
			.append("return null;")
			.append(PropertyConst.LINE_SEPARATOR);
		method.setBody(buf.toString());
		methodList.add(method);
		List<Method> getSetMethodList = getGetSetMethodList(propertyList);
		for (Method m : getSetMethodList) {
			methodList.add(m);
		}
		
		Clazz cls = new Clazz();
		cls.setPackageName(packageName);
		ImportList importList = new ImportList();
		importList.add("com.dalin.framework.action.StrutsTwoAction");
		cls.setImportList(importList);
		cls.setClassName(upperJavaName + "DelAction");
		cls.setExtendsClassName("StrutsTwoAction");
		cls.setPropertyList(propertyList);
		cls.setMethodList(methodList);
		return cls;
	}

	public Clazz getFindActionClazz(String serviceName, Long tableId,
			String packageName) throws Exception {
		EtlTableInfoEntity tableEnty = (EtlTableInfoEntity) getDao()
				.findObjById(EtlTableInfoEntity.class, tableId);
		String tableName = tableEnty.getTableName();
		String serviceRefName = serviceName.substring(0, 1).toLowerCase() 
				+ serviceName.substring(1);
		String upperJavaName = StringUtils.underlineToUpperCamelCase(tableName);
		PropertyList propertyList = new PropertyList();
		propertyList.add(new Property(serviceName, serviceRefName, null));
		propertyList.add(new Property("Long", "id", null));
		List<Method> methodList = new ArrayList<Method>();
		Method method = new Method("execute");
		method.setRtnType(PropertyConst.STRING_STR);
		method.setException("Exception");
		StringBuffer buf = new StringBuffer();
		buf.append(PropertyConst.TWO_TAB)
			.append("super.printInfo(\"\", \"===== this action begin\");")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB)
			.append("try {").append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.THREE_TAB).append(upperJavaName)
			.append("Vo vo = ").append(serviceRefName)
			.append(".find").append(upperJavaName)
			.append("ById(id);").append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.THREE_TAB)
			.append("setResContent(vo);")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB)
			.append("} catch (Exception e) {")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.THREE_TAB)
			.append("e.printStackTrace();")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB)
			.append("}")
			.append(PropertyConst.LINE_SEPARATOR);	
		buf.append(PropertyConst.TWO_TAB)
			.append("super.printInfo(\"\", \"===== this action end\");")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB)
			.append("return null;")
			.append(PropertyConst.LINE_SEPARATOR);
		method.setBody(buf.toString());
		methodList.add(method);
		
		methodList.addAll(getGetSetMethodList(propertyList));
		
		Clazz cls = new Clazz();
		cls.setPackageName(packageName);
		ImportList importList = new ImportList();
		importList.add("com.dalin.framework.action.StrutsTwoAction");
		cls.setImportList(importList);
		cls.setClassName(upperJavaName + "FindAction");
		cls.setExtendsClassName("StrutsTwoAction");
		cls.setPropertyList(propertyList);
		cls.setMethodList(methodList);
		return cls;
	}

	public Clazz getQueryActionClazz(String serviceName, Long tableId,
			String packageName) throws Exception {
		EtlTableInfoEntity tableEnty = (EtlTableInfoEntity) getDao()
				.findObjById(EtlTableInfoEntity.class, tableId);
		String tableName = tableEnty.getTableName();
		String serviceRefName = serviceName.substring(0, 1).toLowerCase() 
				+ serviceName.substring(1);
		String upperJavaName = StringUtils.underlineToUpperCamelCase(tableName);
		PropertyList propertyList = new PropertyList();
		propertyList.add(new Property(serviceName, serviceRefName, null));
		propertyList.add(new Property("String", "condStr", "查询条件"));
		propertyList.add(new Property("int", "start", "查询起始行数"));
		propertyList.add(new Property("int", "limit", "每页显示行数"));
		List<Method> methodList = new ArrayList<Method>();
		Method method = new Method("execute");
		method.setRtnType(PropertyConst.STRING_STR);
		method.setException("Exception");
		StringBuffer buf = new StringBuffer();
		buf.append(PropertyConst.TWO_TAB)
			.append("super.printInfo(\"\", \"===== this action begin\");")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB)
			.append("try {").append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.THREE_TAB).append(upperJavaName)
			.append("Condition cond = (").append(upperJavaName)
			.append("Condition) getCond(").append(upperJavaName)
			.append("Condition.class, condStr, start, limit);")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.THREE_TAB).append("PageInfo pageInfo = ")
			.append(serviceRefName).append(".query").append(upperJavaName)
			.append("s(cond);").append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.THREE_TAB)
			.append("setResContent(pageInfo);")
			.append(PropertyConst.LINE_SEPARATOR);
		
		buf.append(PropertyConst.TWO_TAB)
			.append("} catch (Exception e) {")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.THREE_TAB)
			.append("e.printStackTrace();")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB)
			.append("}")
			.append(PropertyConst.LINE_SEPARATOR);	
		buf.append(PropertyConst.TWO_TAB)
			.append("super.printInfo(\"\", \"===== this action end\");")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB)
			.append("return null;")
			.append(PropertyConst.LINE_SEPARATOR);
		method.setBody(buf.toString());
		methodList.add(method);
		
		methodList.addAll(getGetSetMethodList(propertyList));
		
		Clazz cls = new Clazz();
		cls.setPackageName(packageName);
		ImportList importList = new ImportList();
		importList.add("com.dalin.framework.action.StrutsTwoAction");
		cls.setImportList(importList);
		cls.setClassName(upperJavaName + "QueryAction");
		cls.setExtendsClassName("StrutsTwoAction");
		cls.setPropertyList(propertyList);
		cls.setMethodList(methodList);
		return cls;
	}

	public String getGridJspContent(Long tableId) {
		EtlTableInfoEntity tableEnty = (EtlTableInfoEntity) getDao()
				.findObjById(EtlTableInfoEntity.class, tableId);
		List<EtlColumnInfoEntity> columnList = tableEnty.getEtlColumnInfoEntitys();
		String tableName = tableEnty.getTableName();
		String javaTableName = StringUtils
				.underlineToUpperCamelCase(tableName);
		String divId1 = AutocodeConst.getNextNjlId();
		String divId2 = AutocodeConst.getNextNjlId();
		
		StringBuffer sb = new StringBuffer();
		sb.append("<%@ page language=\"java\" pageEncoding=\"UTF-8\" %>")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append("<html><head><title></title></head><body>")
			    .append(PropertyConst.LINE_SEPARATOR);
		sb.append("<div id=\"").append(divId1)
				.append("\"></div>").append(PropertyConst.LINE_SEPARATOR);
		sb.append("<div id=\"").append(divId2)
				.append("\"></div>").append(PropertyConst.LINE_SEPARATOR);
		sb.append("</body></html>").append(PropertyConst.LINE_SEPARATOR)
		.append(PropertyConst.LINE_SEPARATOR);
		
		sb.append("<script type=\"text/javascript\">")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(javaTableName).append("SearchFormPanel = function() {")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TAB).append(javaTableName)
				.append("SearchFormPanel.superclass.constructor.call(this, {")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TWO_TAB).append("items       : [{")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.THREE_TAB)
				.append("xtype       : 'fieldset',")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.THREE_TAB)
				.append("title       : '查询过滤条件',")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.THREE_TAB)
				.append("collapsed   : true,")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.THREE_TAB)
				.append("collapsible : true,")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.THREE_TAB)
				.append("layout      : 'column',")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.THREE_TAB)
				.append("items       : [{")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.FOUR_TAB).append("columnWidth : .33,")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.FOUR_TAB).append("layout      : 'form',")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.FOUR_TAB).append("items		: [")
				.append(PropertyConst.LINE_SEPARATOR);
		for (EtlColumnInfoEntity cenn : columnList) {
			EtlColumnViewEntity viewEnty = cenn.getColumnViewEntity();
			if (viewEnty == null) continue;
			sb.append(PropertyConst.FIVE_TAB);
			if (1 == viewEnty.getDisplayWay()) {
				sb.append("getTextfield('").append(cenn.getColumnComment())
				.append("', '").append(StringUtils
						.underlineToLowerCamelCase(cenn.getColumnName()))
				.append("')").append(PropertyConst.LINE_SEPARATOR);
			} else if (2 == viewEnty.getDisplayWay()) {
				sb.append("getCombo('").append(cenn.getColumnComment())
				.append("', '").append(StringUtils
						.underlineToLowerCamelCase(cenn.getColumnName()))
				.append("',").append(viewEnty.getDicTypeCode())
				.append(")").append(PropertyConst.LINE_SEPARATOR);
			} else if (5 == viewEnty.getDisplayWay()) {
				sb.append("getDatefield('").append(cenn.getColumnComment())
				.append("', '").append(StringUtils
						.underlineToLowerCamelCase(cenn.getColumnName()))
				.append("')").append(PropertyConst.LINE_SEPARATOR);
			} 
		}
		
		sb.append(PropertyConst.FOUR_TAB).append("]")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.THREE_TAB).append("}, {")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.FOUR_TAB).append("columnWidth : .33,")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.FOUR_TAB).append("layout      : 'form',")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.FOUR_TAB).append("items		: [")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.FOUR_TAB).append("]")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.THREE_TAB).append("}, {")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.FOUR_TAB).append("columnWidth : .33,")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.FOUR_TAB).append("layout      : 'form',")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.FOUR_TAB).append("items		: [")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.FOUR_TAB).append("]")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.THREE_TAB).append("}]")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TWO_TAB).append("}]")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TAB).append("});")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append("}").append(PropertyConst.LINE_SEPARATOR);
		sb.append("Ext.extend(").append(javaTableName)
				.append("SearchFormPanel, Ext.dalin.ElementFormPanel, {});")
				.append(PropertyConst.LINE_SEPARATOR)
				.append(PropertyConst.LINE_SEPARATOR);
		
		sb.append(javaTableName).append("QueryGridPanel = Ext.extend(Ext.dalin.JsonGrid, {")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TAB).append("queryUrl      : '/etl/databaseInfoQuery.html',")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TAB).append("addUrl        : '/platjsp/etl/dbInfoAddMod.jsp?1=1',")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TAB).append("modUrl        : '/platjsp/etl/dbInfoAddMod.jsp?1=1',")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TAB).append("delUrl        : '/etl/databaseInfoDel.html',")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TAB).append("dialogWidth   : 600,")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TAB).append("dialogHeight  : 298,")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TAB).append("headerConfig  : [")
				.append(PropertyConst.LINE_SEPARATOR);
		for (EtlColumnInfoEntity cenn : columnList) {
			sb.append(PropertyConst.TWO_TAB);
			sb.append("{fieldLabel: '")
				.append(cenn.getColumnComment())
				.append("', name: '")
				.append(StringUtils.underlineToLowerCamelCase(cenn.getColumnName())).append("'},")
				.append(PropertyConst.LINE_SEPARATOR);
		}
		sb.append(PropertyConst.TAB).append("]").append(PropertyConst.LINE_SEPARATOR);
		sb.append("});")
				.append(PropertyConst.LINE_SEPARATOR)
				.append(PropertyConst.LINE_SEPARATOR);
		
		sb.append("Ext.onReady(function(){").append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TAB).append("var dalinForm = new ")
				.append(javaTableName).append("SearchFormPanel();")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TAB).append("dalinForm.render('").append(divId1)
				.append("');")
				.append(PropertyConst.LINE_SEPARATOR)
				.append(PropertyConst.LINE_SEPARATOR);
		
		sb.append(PropertyConst.TAB).append("var dalinGrid = new ")
				.append(javaTableName).append("QueryGridPanel();")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TAB).append("var topToolBar = dalinGrid.getTopToolbar();")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TAB).append("topToolBar.add({")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TWO_TAB).append("text    : '查询',")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TWO_TAB).append("tooltip : '将根据您所填写的查询条件进行查询!!!',")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TWO_TAB).append("iconCls : 'icon-query',")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TWO_TAB).append("handler : queryByDemand")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TAB).append("}, '-');")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TAB).append("topToolBar.add({")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TWO_TAB).append("text    : '重置',").append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TWO_TAB).append("tooltip : '将清空查询条件数据恢复到页面第一次加载时的状态!!!',").append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TWO_TAB).append("iconCls : 'icon-resetQuery',").append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TWO_TAB).append("handler : function() {").append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.THREE_TAB).append("dalinForm.form.reset();").append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.THREE_TAB).append("queryByDemand();").append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TWO_TAB).append("}").append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TAB).append("}, '-');").append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TAB).append("dalinGrid.render('")
				.append(divId2).append("');")
				.append(PropertyConst.LINE_SEPARATOR)
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append(PropertyConst.TAB).append("function queryByDemand() {").append(PropertyConst.LINE_SEPARATOR);
		sb.append("		dalinGrid.refresh(dalinForm.form.getValues());").append(PropertyConst.LINE_SEPARATOR);
		sb.append("	}").append(PropertyConst.LINE_SEPARATOR);
		sb.append("});").append(PropertyConst.LINE_SEPARATOR);
		sb.append("</script>").append(PropertyConst.LINE_SEPARATOR);
		return sb.toString();
	}

	public String getAddModJspContent(Long tableId) {
		EtlTableInfoEntity tableEnty = (EtlTableInfoEntity) getDao()
				.findObjById(EtlTableInfoEntity.class, tableId);
		List<EtlColumnInfoEntity> columnList = tableEnty.getEtlColumnInfoEntitys();
		String tableName = tableEnty.getTableName();
		String javaTableName = StringUtils
				.underlineToUpperCamelCase(tableName);
		String divId1 = AutocodeConst.getNextNjlId();
		
		StringBuffer sb = new StringBuffer();
		sb.append("<%@ page language=\"java\" pageEncoding=\"UTF-8\" %>")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append("<%String id = request.getParameter(\"id\");%>")
			    .append(PropertyConst.LINE_SEPARATOR);
		sb.append("<html><head><title></title></head><body>")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append("<div id=\"").append(divId1)
				.append("\"></div>").append(PropertyConst.LINE_SEPARATOR);
		sb.append("</body></html>")
				.append(PropertyConst.LINE_SEPARATOR)
				.append(PropertyConst.LINE_SEPARATOR);
		
		sb.append("<script type=\"text/javascript\">");
		sb.append(javaTableName).append("AddModFormPanel = function() {")
				.append(PropertyConst.LINE_SEPARATOR);
		for (EtlColumnInfoEntity ce : columnList) {
			EtlColumnViewEntity viewEnty = ce.getColumnViewEntity();
			if (viewEnty == null) continue;
			sb.append("	var ").append(StringUtils.underlineToLowerCamelCase(
					ce.getColumnName())).append(" = ");
			if (1 == viewEnty.getDisplayWay()) {
				sb.append("getTextfield('").append(ce.getColumnComment())
						.append("', 'vo.").append(StringUtils
						.underlineToLowerCamelCase(ce.getColumnName()))
						.append("', ").append(ce.getColumnLength() / 2 )
						.append(", ").append(ce.getNullable() == 1 ? true : false).append(");");
			}  else if (2 == viewEnty.getDisplayWay()) {
				sb.append("getCombo('").append(ce.getColumnComment())
				.append("', 'vo.").append(StringUtils
						.underlineToLowerCamelCase(ce.getColumnName()))
				.append("',").append(viewEnty.getDicTypeCode())
				.append(", ").append(ce.getNullable() == 1 ? true : false)
				.append(")").append(PropertyConst.LINE_SEPARATOR);
			} else if (3 == viewEnty.getDisplayWay()) {
				sb.append("getTextarea('").append(ce.getColumnComment())
						.append("', 'vo.").append(StringUtils
						.underlineToLowerCamelCase(ce.getColumnName()))
						.append("', ").append(ce.getColumnLength() / 2 )
						.append(", ").append(ce.getNullable() == 1 ? true : false)
						.append(");").append(PropertyConst.LINE_SEPARATOR);
				sb.append(StringUtils.underlineToLowerCamelCase(ce.getColumnName()))
						.append(".anchor = '96%';");
			} else if (4 == viewEnty.getDisplayWay()) {
				sb.append("getHidden('").append("vo.").append(StringUtils
						.underlineToLowerCamelCase(ce.getColumnName()))
						.append("');");
			} else if (5 == viewEnty.getDisplayWay()) {
				sb.append("getDatefield('").append(ce.getColumnComment())
						.append("', 'vo.").append(StringUtils
						.underlineToLowerCamelCase(ce.getColumnName()))
						.append("', ").append(ce.getColumnLength() / 2 )
						.append(", ").append(ce.getNullable() == 1 ? true : false)
						.append(");");
			}
			sb.append(PropertyConst.LINE_SEPARATOR);
		}
		sb.append(PropertyConst.LINE_SEPARATOR);
		
		sb.append(PropertyConst.TAB).append(javaTableName)
				.append("AddModFormPanel.superclass.constructor.call(this, {")
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append("		reader  : new Ext.data.JsonReader({root  : 'data' }, [")
				.append(PropertyConst.LINE_SEPARATOR);
		for (EtlColumnInfoEntity ce : columnList) {
			EtlColumnViewEntity viewEnty = ce.getColumnViewEntity();
			sb.append("       		{ name: 'vo.").append(StringUtils
					.underlineToLowerCamelCase(ce.getColumnName()))
					.append("', mapping: '").append(StringUtils
					.underlineToLowerCamelCase(ce.getColumnName()))
					.append("' },").append(PropertyConst.LINE_SEPARATOR);
		}
		sb.append("    	]),").append(PropertyConst.LINE_SEPARATOR);
		sb.append("		items       : [{").append(PropertyConst.LINE_SEPARATOR);
		sb.append("        	layout         : 'column',").append(PropertyConst.LINE_SEPARATOR);
		sb.append("			items		   : [{").append(PropertyConst.LINE_SEPARATOR);
		sb.append("            	columnWidth : .5,").append(PropertyConst.LINE_SEPARATOR);
		sb.append("           	 	layout      : 'form',").append(PropertyConst.LINE_SEPARATOR);
		sb.append("	            items		: [").append(PropertyConst.LINE_SEPARATOR);
		for (EtlColumnInfoEntity ce : columnList) {
			sb.append(StringUtils.underlineToLowerCamelCase(ce.getColumnName()))
					.append(",").append(PropertyConst.LINE_SEPARATOR);
		}
		sb.append("	            ]").append(PropertyConst.LINE_SEPARATOR);
		sb.append("			}, {").append(PropertyConst.LINE_SEPARATOR);
		sb.append("            	columnWidth : .5,").append(PropertyConst.LINE_SEPARATOR);
		sb.append("          	 	layout      : 'form',").append(PropertyConst.LINE_SEPARATOR);
		sb.append("	            items		: [").append(PropertyConst.LINE_SEPARATOR);
		sb.append("	            ]").append(PropertyConst.LINE_SEPARATOR);
		sb.append("	        }]").append(PropertyConst.LINE_SEPARATOR);
		sb.append("    	}]").append(PropertyConst.LINE_SEPARATOR);
		sb.append("	});").append(PropertyConst.LINE_SEPARATOR);
		sb.append("}").append(PropertyConst.LINE_SEPARATOR);
		sb.append("Ext.extend(").append(javaTableName)
				.append("AddModFormPanel, Ext.dalin.ElementFormPanel, {});")
				.append(PropertyConst.LINE_SEPARATOR)
				.append(PropertyConst.LINE_SEPARATOR);
		sb.append("Ext.onReady(function(){").append(PropertyConst.LINE_SEPARATOR);
		sb.append("	var dalinForm = new ").append(javaTableName)
				.append("AddModFormPanel();").append(PropertyConst.LINE_SEPARATOR);
		sb.append("	dalinForm.addButton('保存', function() {").append(PropertyConst.LINE_SEPARATOR);
		sb.append("		dalinForm.submitForm('AddMod.html', function(form, action) {").append(PropertyConst.LINE_SEPARATOR);
		sb.append("			publicDialog.close();").append(PropertyConst.LINE_SEPARATOR);
		sb.append("		});").append(PropertyConst.LINE_SEPARATOR);
		sb.append("	}, dalinForm);").append(PropertyConst.LINE_SEPARATOR);
		sb.append("	dalinForm.addButton('重置', function(){").append(PropertyConst.LINE_SEPARATOR);
		sb.append("		dalinForm.getForm().reset();").append(PropertyConst.LINE_SEPARATOR);
		sb.append("	}, dalinForm);")
				.append(PropertyConst.LINE_SEPARATOR)
				.append(PropertyConst.LINE_SEPARATOR);
		
		sb.append("	dalinForm.on('render', function() {").append(PropertyConst.LINE_SEPARATOR);
		sb.append("		dalinForm.loadDataById('<%=id%>', '/Find.html?id=<%=id%>',function(){").append(PropertyConst.LINE_SEPARATOR);
		sb.append("			").append(PropertyConst.LINE_SEPARATOR);
		sb.append("		});").append(PropertyConst.LINE_SEPARATOR);
		sb.append("	});").append(PropertyConst.LINE_SEPARATOR);
		sb.append("	dalinForm.render('").append(divId1).append("');").append(PropertyConst.LINE_SEPARATOR);
		sb.append("});").append(PropertyConst.LINE_SEPARATOR);
		sb.append("</script>").append(PropertyConst.LINE_SEPARATOR);
		return sb.toString();
	}
}